﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Linq;
using System.Data.Objects;
using System.Data.Query;
using System.Data.Metadata;
using System.Data.Mapping;

namespace OrmAuto
{
    public class QuerierAnnunciSql : ABaseSql
    {
        /// <summary>
        /// Prova da sistemare
        /// </summary>
        /// <returns></returns>
        public object getAnnVendita()
        {
            using (var DAL = getSQLConnection("baseConnectionString"))
            {
                var comm = getSqlCommand("select * from AnnVendita", CommandType.Text, DAL);
                using (var dr = comm.ExecuteReader())
                {
                    //todo : sistemare
                    ClassCreator cc = new ClassCreator();
                    var classe = cc.getClassedinamica(dr, "prova", "prova");

                    var tt = classe.GetType();
                    var dy = DynamicBuilder.CreateBuilderSafeNull(dr, classe);

                    while (dr.Read())
                    {
                        var result = dy.Build(dr);
                    }
                    return null;
                }
            }
        }//fM

        public DataTable get_vAnnVenditaCompleta(int? pIDAnnuncio)
        {
            using (var DAL = getSQLConnection("baseConnectionString"))
            {
                string sql = "select * from vAnnVenditaCompleta";
                if (pIDAnnuncio.HasValue)
                    sql += " where ID = "+ pIDAnnuncio.Value.ToString()   ;
                using (var comm = getSqlCommand(sql,
                    CommandType.Text, DAL))
                {
                    using (var dr = comm.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        DataTable dt = new DataTable();
                        dt.Load(dr);
                        return dt;
                    }
                }
            }
        }//fM

        public DataTable get_vAnnAcquistoCompleta(int? pIDAnnuncio)
        {
            using (var DAL = getSQLConnection("baseConnectionString"))
            {
                string sql = "select * from vAnnAcquistoCompleta";
                if (pIDAnnuncio.HasValue)
                    sql += " where ID = " + pIDAnnuncio.Value.ToString();
                using (var comm = getSqlCommand(sql,
                    CommandType.Text, DAL))
                {
                    using (var dr = comm.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        DataTable dt = new DataTable();
                        dt.Load(dr);
                        return dt;
                    }
                }
            }
        }//fM


    }//fC
}//fN
